﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BLL;
using PaymentBLL;

/// <summary>
/// Summary description for WSPaymentConfig
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WSPaymentConfig : System.Web.Services.WebService {

    public WSPaymentConfig () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public int GateWayUpdate(int id,int amountIn,double reciveAu,double reciveAg,int vndIn,int maxVndIn,int status)
    {
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.Edit) != (int)Config.EnumPermission.Edit)
            return -1;
        var ctr = new CtrPaymentGateway();
        var isActiveIn = status > 0;
        int result = ctr.UpdatePaymentGatewayIn(id, amountIn, vndIn, maxVndIn, reciveAu, reciveAg, isActiveIn);
        if (result > 0)
        {
            string des = "Cập nhật cổng thanh toán: [id: " + id + ", amountIn: " + amountIn + ", reciveAu: " + reciveAu + ", reciveAg: " + reciveAg + ", vndIn: " + vndIn + ", maxVndIn: " + maxVndIn + ", isActiveIn: " + isActiveIn + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }
    [WebMethod]
    public int GateWayUpdateOut(int id, double auOut, double reciveAmount,int outVND, int status)
    {
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.Edit) != (int)Config.EnumPermission.Edit)
            return -1;
        var ctr = new CtrPaymentGateway();
        var isActiveOut = status > 0;
        int result = ctr.UpdatePaymentGatewayOut(id, auOut, 0, reciveAmount, outVND, 0, isActiveOut);
        if (result > 0)
        {
            string des = "Cập nhật cổng thanh toán: [id: " + id + ", auOut: " + auOut + ", reciveAmount: " + reciveAmount +", outVND: "+outVND+ ", isActiveOut: " + isActiveOut + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }
    [WebMethod]
    public int AgToAuUpdate(int fromAg, int toAu,int status)
    {
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.Edit) != (int)Config.EnumPermission.Edit)
            return -1;
        var ctr = new CtrConvertAuAg();
        var isActiveOut = status > 0;
        int result = ctr.UpdateConvertAgToAg(fromAg, toAu, isActiveOut);
        if (result > 0)
        {
            string des = "Cập nhật đổi bạc ra vàng: [fromAg: " + fromAg + ", toAu: " + toAu + ", isActiveOut: " + isActiveOut + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }
    #region ItemIn
    [WebMethod]
    public int ItemInInsert(int paymentGatewayId,string name,string description,int amountIn,int vndIn,int receiveAu,
                                                      int receiveAg,int quantity,int status)
    {
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.New) != (int)Config.EnumPermission.New)
            return -1;

        var ctr = new CtrItem();        
        int result = ctr.ItemInInsert(paymentGatewayId, name, description, amountIn, vndIn, receiveAu,
                                                      receiveAg, quantity, status);
        if (result > 0)
        {
            string des = "Thêm mới gói vàng: [paymentGatewayId: " + paymentGatewayId + ", name: " + name + ", description: " + description + ", amountIn: " + amountIn
                + ", vndIn: " + vndIn + ", receiveAu: " + receiveAu + ", receiveAg: " + receiveAg + ", quantity: " + quantity + ", status: " + status
                + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Add, des, function);
        }
        return result;
    }

    [WebMethod]
    public int ItemInUpdate(int id,int paymentGatewayId, string name, string description, int amountIn, int vndIn, int receiveAu,
                                                      int receiveAg, int quantity, int status)
    {
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.Edit) != (int)Config.EnumPermission.Edit)
            return -1;

        var ctr = new CtrItem();        
        int result = ctr.ItemInUpdate(id,paymentGatewayId, name, description, amountIn, vndIn, receiveAu,
                                                      receiveAg, quantity, status);
        if (result > 0)
        {
            string des = "Cập nhật gói vàng: [id: "+id+", paymentGatewayId: " + paymentGatewayId + ", name: " + name + ", description: " + description + ", amountIn: " + amountIn
                + ", vndIn: " + vndIn + ", receiveAu: " + receiveAu + ", receiveAg: " + receiveAg + ", quantity: " + quantity + ", status: " + status
                + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }
    [WebMethod]
    public int ItemInUpdateStatus(int id, int status)
    {
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.Edit) != (int)Config.EnumPermission.Edit)
            return -1;

        var ctr = new CtrItem();
        int result = ctr.ItemInUpdateStatus(id, status);
        if (result > 0)
        {
            string des = "Cập nhật trạng thái gói vàng: [id: " + id + ", status: " + status+ "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }

    [WebMethod]
    public int ItemInDelete(int id)
    {
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.Delete) != (int)Config.EnumPermission.Delete)
            return -1;

        var ctr = new CtrItem();
        int result = ctr.ItemInDelete(id);
        if (result > 0)
        {
            string des = "Xóa gói vàng: [id: " + id + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Delete, des, function);
        }
        return result;
    }
    #endregion

    #region ItemOut
    [WebMethod]
    public int ItemOutInsert(int paymentGatewayId, string name, string description, int auOut, int agOut, int receiveAmount,
                                                      int vndOut, int quantity, int status)
    {
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.New) != (int)Config.EnumPermission.New)
            return -1;

        var ctr = new CtrItem();
        int result = ctr.ItemOutInsert(paymentGatewayId, name, description, auOut, agOut,receiveAmount,
                                                      vndOut, quantity, status);
        if (result > 0)
        {
            string des = "Thêm mới gói ra: [paymentGatewayId: " + paymentGatewayId + ", name: " + name + ", description: " + description + ", auOut: " + auOut
                + ", agOut: " + agOut + ", receiveAmount: " + receiveAmount + ", vndOut: " + vndOut + ", quantity: " + quantity + ", status: " + status
                + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Add, des, function);
        }
        return result;
    }

    [WebMethod]
    public int ItemOutUpdate(int id, int paymentGatewayId, string name, string description, int auOut, int agOut, int receiveAmount,
                                                      int vndOut, int quantity, int status)
    {
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.Edit) != (int)Config.EnumPermission.Edit)
            return -1;

        var ctr = new CtrItem();
        int result = ctr.ItemOutUpdate(id, paymentGatewayId, name, description, auOut, agOut, receiveAmount,
                                                      vndOut, quantity, status);
        if (result > 0)
        {
            string des = "Cập nhật gói ra: [id: " + id + ", paymentGatewayId: " + paymentGatewayId + ", name: " + name + ", description: " + description + ", auOut: " + auOut
                + ", agOut: " + agOut + ", receiveAmount: " + receiveAmount + ", vndOut: " + vndOut + ", quantity: " + quantity + ", status: " + status
                + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }
    [WebMethod]
    public int ItemOutUpdateStatus(int id, int status)
    {
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.Edit) != (int)Config.EnumPermission.Edit)
            return -1;

        var ctr = new CtrItem();
        int result = ctr.ItemOutUpdateStatus(id, status);
        if (result > 0)
        {
            string des = "Cập nhật trạng thái gói ra: [id: " + id + ", status: " + status + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }

    [WebMethod]
    public int ItemOutDelete(int id)
    {
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.Delete) != (int)Config.EnumPermission.Delete)
            return -1;

        var ctr = new CtrItem();
        int result = ctr.ItemOutDelete(id);
        if (result > 0)
        {
            string des = "Xóa gói ra: [id: " + id + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Delete, des, function);
        }
        return result;
    }
    #endregion
}
